//
//  OptionMenuView.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/25.
//
//

#include "OptionMenuView.h"
#include "ShowHelpScreen.h"
#include "MusicController.h"

USING_NS_CC;

#define BACK_MENU_X    130
#define BACK_MENU_Y    2
#define HELP_MENU_X    114
#define HELP_MENU_Y    46
#define ABOUT_MENU_X    85
#define ABOUT_MENU_Y    85
#define MUSIC_MENU_X    46
#define MUSIC_MENU_Y    114
#define RING_MENU_X    2
#define RING_MENU_Y    130

typedef enum {
    ZBg = 0,
    ZMenu,

} Zindex;

typedef enum {
    TagOptionMenu = 1,
    Tagbg,
    TagMenuNormal,
    TagMenuOut,
    TagMenuBack,
    TagMenuAbout,
    TagMenuMusic,
    TagMenuRing,
    TagMenuNoMusic,
    TagMenuNoRing,
    TagMenuHelp,
    
    
} MenuTag;

OptionMenuView::OptionMenuView()
:bg(NULL)
,isMusic(false)
{
}

bool OptionMenuView::init()
{
    if (!Layer::init()) {
        return false;
    }

    bgInit();
    controllMenuInit();
    
    return true;
}

void OptionMenuView::bgInit()
{
    bg = Sprite::create("MenuBg.png");
    bg->setAnchorPoint(Vec2::ZERO);
    bg->setPosition(Vec2::ZERO);
    bg->setTag(Tagbg);
    addChild(bg,ZBg);
    bg->setVisible(false);
}

void OptionMenuView::controllMenuInit()
{
    auto menuItem = MenuItemImage::create("MenuNormal.png", "MenuNormal.png","MenuNormal.png", CC_CALLBACK_1(OptionMenuView::menuCallback, this));
    menuItem->setTag(TagMenuNormal);
    menuItem->setAnchorPoint(Vec2::ZERO);
    menuItem->setPosition(Vec2::ZERO);

    
    auto menuItem2 = MenuItemImage::create("MenuOut.png", "MenuOut.png","MenuOut.png", CC_CALLBACK_1(OptionMenuView::menuCallback, this));
    menuItem2->setTag(TagMenuOut);
    menuItem2->setAnchorPoint(Vec2::ZERO);
    menuItem2->setPosition(Vec2::ZERO);
    menuItem2->setVisible(false);
    
    auto menuItemBack = MenuItemImage::create("MenuBackNormal.png", "MenuBackSelected.png","MenuBackNormal.png", CC_CALLBACK_1(OptionMenuView::subMenuCallback, this));
    menuItemBack->setTag(TagMenuBack);
    menuItemBack->setAnchorPoint(Vec2::ZERO);
    menuItemBack->setPosition(Point(BACK_MENU_X,BACK_MENU_Y));
    menuItemBack->setVisible(false);
    
    auto menuItemHelp = MenuItemImage::create("MenuHelp.png", "MenuHelpSelected.png","MenuHelp.png", CC_CALLBACK_1(OptionMenuView::subMenuCallback, this));
    menuItemHelp->setTag(TagMenuHelp);
    menuItemHelp->setAnchorPoint(Vec2::ZERO);
    menuItemHelp->setPosition(Point(HELP_MENU_X,HELP_MENU_Y));
    menuItemHelp->setVisible(false);
    
    auto menuItemAbout = MenuItemImage::create("MenuAboutNormal.png", "MenuAboutSelected.png","MenuAboutNormal.png", CC_CALLBACK_1(OptionMenuView::subMenuCallback, this));
    menuItemAbout->setTag(TagMenuAbout);
    menuItemAbout->setAnchorPoint(Vec2::ZERO);
    menuItemAbout->setPosition(Point(ABOUT_MENU_X,ABOUT_MENU_Y));
    menuItemAbout->setVisible(false);

    auto menuItemMusic = MenuItemImage::create("MenuMusicNormal.png", "MenuMusicSelected.png","MenuMusicNormal.png", CC_CALLBACK_1(OptionMenuView::subMenuCallback, this));
    menuItemMusic->setTag(TagMenuMusic);
    menuItemMusic->setAnchorPoint(Vec2::ZERO);
    menuItemMusic->setPosition(Point(MUSIC_MENU_X,MUSIC_MENU_Y));
    menuItemMusic->setVisible(false);

    auto menuItemNoMusic = MenuItemImage::create("MenuNoMusic.png", "MenuNoMusicSelected.png","MenuNoMusic.png", CC_CALLBACK_1(OptionMenuView::subMenuCallback, this));
    menuItemNoMusic->setTag(TagMenuNoMusic);
    menuItemNoMusic->setAnchorPoint(Vec2::ZERO);
    menuItemNoMusic->setPosition(Point(MUSIC_MENU_X,MUSIC_MENU_Y));
    menuItemNoMusic->setVisible(false);

    auto menuItemRing = MenuItemImage::create("MenuRingNormal.png", "MenuRingSelected.png","MenuRingNormal.png", CC_CALLBACK_1(OptionMenuView::subMenuCallback, this));
    menuItemRing->setTag(TagMenuRing);
    menuItemRing->setAnchorPoint(Vec2::ZERO);
    menuItemRing->setPosition(Point(RING_MENU_X,RING_MENU_Y));
    menuItemRing->setVisible(false);

    auto menuItemNoRing = MenuItemImage::create("MenuNoRing.png", "MenuNoRingSelected.png","MenuNoRing.png", CC_CALLBACK_1(OptionMenuView::subMenuCallback, this));
    menuItemNoRing->setTag(TagMenuNoRing);
    menuItemNoRing->setAnchorPoint(Vec2::ZERO);
    menuItemNoRing->setPosition(Point(RING_MENU_X,RING_MENU_Y));
    menuItemNoRing->setVisible(false);

    auto menu = Menu::create(menuItem,menuItem2,menuItemBack,menuItemHelp,
    		menuItemAbout,menuItemMusic,menuItemNoMusic,menuItemRing,menuItemNoRing,NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Vec2::ZERO);
    menu->setTag(TagOptionMenu);
    addChild(menu,ZMenu);
}

void OptionMenuView::menuCallback(Ref *sender)
{
    Menu* menu = (Menu*)this->getChildByTag(TagOptionMenu);
    MenuItemImage* menuItem = (MenuItemImage*)menu->getChildByTag(TagMenuNormal);
    MenuItemImage* menuItemOut = (MenuItemImage*)menu->getChildByTag(TagMenuOut);
    MenuItemImage* menuItemBack = (MenuItemImage*)menu->getChildByTag(TagMenuBack);
    MenuItemImage* menuItemHelp = (MenuItemImage*)menu->getChildByTag(TagMenuHelp);
    MenuItemImage* menuItemAbout = (MenuItemImage*)menu->getChildByTag(TagMenuAbout);
    MenuItemImage* menuItemMusic = (MenuItemImage*)menu->getChildByTag(TagMenuMusic);
    MenuItemImage* menuItemRing = (MenuItemImage*)menu->getChildByTag(TagMenuRing);
    MenuItemImage* menuItemNoMusic = (MenuItemImage*)menu->getChildByTag(TagMenuNoMusic);
    MenuItemImage* menuItemNoRing = (MenuItemImage*)menu->getChildByTag(TagMenuNoRing);

    if(menuItem->isVisible()){
        menuItem->setVisible(false);
        menuItemOut->setVisible(true);
        bg->setVisible(true);
        menuItemBack->setVisible(true);
        menuItemHelp->setVisible(true);
        menuItemAbout->setVisible(true);
        if(isMusic){
        	menuItemMusic->setVisible(true);
        }else{
        	menuItemNoMusic->setVisible(true);
        }
        if(MusicController::getInstance()->getEffectEnable()){
        	menuItemRing->setVisible(true);
        }else{
        	menuItemNoRing->setVisible(true);
        }

    }else{
        menuItem->setVisible(true);
        menuItemOut->setVisible(false);
        bg->setVisible(false);
        menuItemBack->setVisible(false);
        menuItemHelp->setVisible(false);
        menuItemAbout->setVisible(false);
        menuItemMusic->setVisible(false);
        menuItemRing->setVisible(false);
        menuItemNoMusic->setVisible(false);
        menuItemNoRing->setVisible(false);
    }
    
}

void OptionMenuView::subMenuCallback(Ref *sender)
{
    MenuItemImage *seletedMenu = (MenuItemImage*)sender;

    if(seletedMenu->getTag() == TagMenuBack){
        Director::getInstance()->popScene();
    }else if(seletedMenu->getTag() == TagMenuHelp){
        Director::getInstance()->pushScene(ShowHelpScreen::createScene());
    }else if(seletedMenu->getTag() == TagMenuAbout){

    }else if(seletedMenu->getTag() == TagMenuNoMusic){
    	changeMusicState(true);
    }else if(seletedMenu->getTag() == TagMenuMusic){
    	changeMusicState(false);
    }else if(seletedMenu->getTag() == TagMenuNoRing){
    	changeRingState(true);
    }else if(seletedMenu->getTag() == TagMenuRing){
    	changeRingState(false);
    }
}

void OptionMenuView::changeMusicState(bool flag)
{
	isMusic = flag;
    Menu* menu = (Menu*)this->getChildByTag(TagOptionMenu);
    MenuItemImage* menuItemMusic = (MenuItemImage*)menu->getChildByTag(TagMenuMusic);
    MenuItemImage* menuItemNoMusic = (MenuItemImage*)menu->getChildByTag(TagMenuNoMusic);
    if(isMusic){
    	menuItemMusic->setVisible(true);
    	menuItemNoMusic->setVisible(false);
    }else{
    	menuItemMusic->setVisible(false);
    	menuItemNoMusic->setVisible(true);
    }
}

void OptionMenuView::changeRingState(bool flag)
{
	MusicController::getInstance()->effectEnable(flag);
    Menu* menu = (Menu*)this->getChildByTag(TagOptionMenu);
    MenuItemImage* menuItemRing = (MenuItemImage*)menu->getChildByTag(TagMenuRing);
    MenuItemImage* menuItemNoRing = (MenuItemImage*)menu->getChildByTag(TagMenuNoRing);
    if(MusicController::getInstance()->getEffectEnable()){
    	menuItemRing->setVisible(true);
    	menuItemNoRing->setVisible(false);
    }else{
    	menuItemRing->setVisible(false);
    	menuItemNoRing->setVisible(true);
    }
}


